/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.command;

import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.commands.Command;

import ro.pub.cs.se.moondance.directedit.ForbiddenIdentifierNames;
import ro.pub.cs.se.moondance.model.UMLBaseClass;
import ro.pub.cs.se.moondance.model.UMLPackage;
import ro.pub.cs.se.moondance.model.ModelElement;
import ro.pub.cs.se.moondance.model.UMLDiagram;
import ro.pub.cs.se.moondance.model.UMLEntity;

public class ChangePackageNameCommand extends Command {

	private UMLPackage _package;
	private String name;
	private String oldName;

	public ChangePackageNameCommand(UMLPackage _package) {
		this._package = _package;
	}

	/**
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		_package.setName(name);
	}

	/**
	 * @return whether we can apply changes
	 */
	public boolean canExecute() {

		if (name != null) {
			// verify if the name of the interface is not already used
			ModelElement parent = _package.getParent();
			if (parent instanceof UMLDiagram) {
				UMLDiagram diagram = (UMLDiagram) parent;
				List<UMLEntity> children = diagram.getChildren();
				for (Iterator<UMLEntity> iter = children.iterator(); iter
						.hasNext();) {
					UMLEntity child = iter.next();
					String childName = "";
					if (child instanceof UMLPackage)
						childName = ((UMLPackage) child).getName();
					else if (child instanceof UMLBaseClass)
						childName = ((UMLBaseClass) child).getName();
					if (childName.equals(name))
						if (child instanceof UMLBaseClass)
							return false;
						else if (!(_package == (UMLPackage) child))
							return false;
				}
			} else { // parent is a package
				UMLPackage _package = (UMLPackage) parent;
				if (_package.getName().equals(name))
					return false;
				List<UMLBaseClass> classes = _package.getContainedClasses();
				List<UMLPackage> packages = _package.getContainedPackages();
				for (Iterator<UMLBaseClass> iter = classes.iterator(); iter
						.hasNext();) {
					UMLBaseClass child = iter.next();
					String childName = child.getName();
					if (childName.equals(name))
						return false;
				}
				for (Iterator<UMLPackage> iter = packages.iterator(); iter
						.hasNext();) {
					UMLPackage child = iter.next();
					String childName = child.getName();
					if (childName.equals(name))
						return false;
				}
			}
			return true;
		} else {
			name = oldName;
			return false;
		}
	}

	/**
	 * sets the configuration string : name
	 */

	public void setConfigurationString(String config) {
		name = null;
		if ((config != null) && (config.length() > 0)
				&& (!ForbiddenIdentifierNames.contains(config)))
			name = config;
	}

	/**
	 * methods to set old package name
	 */
	public void setOldName(String string) {
		oldName = string;
	}

	/**
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		_package.setName(oldName);
	}

}